package com.itheima.service;

import com.itheima.pojo.Arrangement;
import com.itheima.pojo.Movie;
import com.itheima.pojo.ShowMovie;

import java.util.ArrayList;
import java.util.Scanner;

public class ArrangementService {

    //创建Scanner扫描器作为成员变量，给本类所有方法使用
    Scanner sc = new Scanner(System.in);
    ArrayList<Movie> list;
    ArrayList<Arrangement> arrayList = new ArrayList<>();



    /**
     * 1.排片设置的操作页功能
     */
    public void ArrangeStart(ArrayList<Movie> list, ArrayList<ShowMovie> showMovies) {
        this.list = list;
        while (true) {
            System.out.println("==================排片设置==================");
            System.out.println("请选择功能：");
            System.out.println("1.电影排片\t2.排片信息\t3.修改排片\t4.删除排片\t5.退出排片");
            String choice = sc.next();
            switch (choice) {
                case "1":
                    movieArrange(showMovies);
                    break;
                case "2":
                    ArrangeInformation();
                    break;
                case "3":
                    ModifyArrange();
                    break;
                case "4":
                    Delete();
                    break;
                case "5":
                    //退出排片，结束排片设置的方法
                    return;
                default:
                    System.out.println("您的选择有误，请重试！");
                    break;
            }
        }
    }

    //排片
    public void movieArrange(ArrayList<ShowMovie> showMovies) {
        int next1;
        double v;
        System.out.println("============电影排片==========");
        System.out.println("请输入电影名称:");
        String next = sc.next();
        for (int i = 0; i < list.size(); i++) {
            if (next.equals(list.get(i).getName())) {
                for (Arrangement arrange : arrayList) {
                    if (next.equals(arrange.getMovieName())) {
                        System.out.println("此影片早已排片,请检查后重试!");
                        return;
                    }
                }

                while (true) {
                    System.out.println("请输入影厅号码,可选择的号码为1-5号");
                    next1 = sc.nextInt();
                    if (next1 >= 1 && next1 <= 5) {
                        break;
                    } else {
                        System.out.println("你输入的号码有误,请重新输入!");
                    }
                }

                while (true) {
                    System.out.println("请输入播放时间:");
                    v = sc.nextDouble();
                    boolean aFalse = isFalse(i, next1, v);
                    if (aFalse) {
                        break;
                    } else {
                        System.out.println("该播放时间和其他电影冲突,请重试!");
                    }
                }

                int duration = list.get(i).getDuration();
                double Etime = duration + v;

                System.out.println("请输入电影票价格:");
                double price = sc.nextDouble();
                Arrangement arrangement = new Arrangement(next1, next, v, Etime, price);
                arrayList.add(arrangement);

                String director = list.get(i).getDirector();
                String actor = list.get(i).getActor();
                String profile = list.get(i).getProfile();
                int duration1 = list.get(i).getDuration();
                String name = list.get(i).getName();
                ShowMovie show = new ShowMovie(name,director,actor,profile,duration1,Etime,price);
                showMovies.add(show);
                System.out.println("电影<" + next + ">排片成功!");
//                int number, String movieName, double startTime, double endTime, double price
                return;

            }
        }
        System.out.println("该电影并未上架,请检查后重试!");


    }

    //  判断时间是否冲突
    public boolean isFalse(int i, int number, Double v) {
        int count = 0;
        ArrayList<Arrangement> li = new ArrayList<>();
        for (Arrangement arrangement : arrayList) {
            if (arrangement.getNumber() == number) {
                li.add(arrangement);
            }
        }

        for (int i1 = 0; i1 < li.size(); i1++) {
            if ((v > li.get(i1).getEndTime()) || ((v + list.get(i).getDuration()) < li.get(i1).getStartTime())) {
                count++;
            }
        }

        return count == li.size();

    }



    //  显示排片信息
    public void ArrangeInformation() {
        int count = 0;
        System.out.println("========排片信息========");
        if (arrayList.size() == 0) {
            System.out.println("暂无排片信息!");
            return;
        }

        for (int i = 0; i < arrayList.size(); i++) {

            if (count == arrayList.size() - 1) {
                System.out.println("电影:" + arrayList.get(i).getMovieName());
                System.out.println("影厅:" + arrayList.get(i).getNumber() + "\t时间:"
                        + arrayList.get(i).getStartTime() + "-" + arrayList.get(i).getEndTime()
                        + "\t售价:" + arrayList.get(i).getPrice());


            } else {
                System.out.println("电影:" + arrayList.get(i).getMovieName());
                System.out.println("影厅:" + arrayList.get(i).getNumber() + "\t时间:"
                        + arrayList.get(i).getStartTime() + "-" + arrayList.get(i).getEndTime()
                        + "\t售价:" + arrayList.get(i).getPrice());
                System.out.println("-------------------");
                count++;
            }
        }
    }

    //   修改排片
    public void ModifyArrange() {
        System.out.println("============修改排片===========");
        System.out.println("请输入要修改电影的名称:");
        String next = sc.next();
        for (Arrangement arrangement : arrayList) {
            if (next.equals(arrangement.getMovieName())) {
                loop:
                while (true) {
                    System.out.println("请选择要修改的信息:1.影厅号码\t2.播放时间\t3.电影票价格\t4.退出");
                    String next1 = sc.next();

                    switch (next1) {
                        case "1":
                            ModifyNum(arrangement);
                            break;
                        case "2":
                            ModifyTime(arrangement);
                            break;
                        case "3":
                            ModifyPrice(arrangement);
                            break;
                        case "4":
                            break loop;
                        default:
                            System.out.println("你输入的信息有误,请重新输入!");
                            break loop;
                    }
                }
                return;
            }
        }
        System.out.println("要修改的电影不存在,请重新选择!!");
    }

    public void ModifyNum(Arrangement arrangement){
        while (true) {
            System.out.println("请输入影厅号码,可选择的号码为1-5号");
            int next1 = sc.nextInt();
            if (next1 >= 1 && next1 <= 5) {
                arrangement.setNumber(next1);
                break;
            } else {
                System.out.println("你输入的号码有误,请重新输入!");
            }
        }
    }
// 修改排片时间
    public void ModifyTime(Arrangement arrangement){
        double v;
        double d;
        while (true) {
            System.out.println("请输入播放时间:");
            v = sc.nextDouble();
            d = arrangement.getEndTime()-arrangement.getStartTime();
            boolean aFalse = isFalse1(v+d,arrangement.getNumber(),v,arrangement);

            if (aFalse) {
                break;
            } else {
                System.out.println("该播放时间和其他电影冲突,请重试!");
            }
        }
        arrangement.setStartTime(v);
        arrangement.setEndTime(v+d);
    }
    // 修改时间判断是否冲突
    public boolean isFalse1(double i, int number, double v,Arrangement arrang) {
        int count = 0;
        ArrayList<Arrangement> li = new ArrayList<>();
        for (Arrangement arrangement : arrayList) {
            if (arrangement.getNumber() == number) {
                li.add(arrangement);
            }
        }
        li.remove(arrang);

        for (int i1 = 0; i1 < li.size(); i1++) {
            if ((v > li.get(i1).getEndTime()) || (i < li.get(i1).getStartTime())) {
                count++;
            }
        }

        return count == li.size();

    }

    public void ModifyPrice(Arrangement arrangement){
        System.out.println("请输入修改的价格为:");
        double v = sc.nextDouble();
        arrangement.setPrice(v);
    }


    public void Delete(){
        System.out.println("请输入要删除的排片名称:");
        String next = sc.next();
        for (int i = 0; i < arrayList.size(); i++) {
            if(next.equals(arrayList.get(i).getMovieName())){
                arrayList.remove(i);
                System.out.println("删除排片成功!");
                return;
            }
        }
        System.out.println("该影片暂未排片,请重新输入!");
    }
}
